
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==nullptr)
        return 0;
       queue<TreeNode*>Q;
       Q.push(root);
       int res=0;
       while(!Q.empty()){
           int size=Q.size();
           while(size>0){
               TreeNode*a=Q.front();
               Q.pop();
               if(a->left)
               Q.push(a->left);
               if(a->right)
               Q.push(a->right);
               size--;
           }
          res++;
       }
       return res;
    }
};